import { type IQuery, type IResponse, type TableResponse } from '@/api/config/types';
import axios from 'axios';
import type { AccountInfo, UserDetail, UserProfile } from './types';

type LoginForm = {
  username: string;
  password: string;
};

interface LoginResponse {
  expire: number;
  type: string;
  token: string;
}
export interface UserRouter {
  id: string;
  path: string;
  hidden: boolean;
  component: string;
  meta: Meta;
  children: UserRouter[];
}

interface Meta {
  title: string;
  icon: string;
  noCache: boolean;
  link: null;
}
const prefix = '/system/user';
// 登录方法
export const login = (data: LoginForm) => {
  return axios.post<IResponse<LoginResponse>>('/login', { ...data });
};
export const userRouters = () => {
  return axios.get<IResponse<UserRouter[]>>('/getRouters');
};

export const getUserProfile = () => {
  return axios.get<IResponse<AccountInfo>>('/system/user/profile');
};

export const getDetails = () => {
  return axios.get<IResponse<string>>('/store/store/details');
};

// 查询用户列表
export const listUser = (query: IQuery) => {
  return axios.get<TableResponse<UserProfile>>(`${prefix}/list`, { params: query });
};

// 新增用户
export const addUser = (data: Partial<UserProfile>) => {
  return axios.post(`${prefix}`, data);
};

// 删除用户
export const delUser = (userId: string[]) => {
  return axios.delete(`${prefix}/${userId.join(',')}`);
};

// 修改用户
export const updateUser = (data: Partial<UserProfile>) => {
  return axios.put(`${prefix}`, data);
};

// 查询用户详细
export const getUser = (userId = '') => {
  return axios.get<IResponse<UserDetail>>(`${prefix}/${userId}`);
};

// 用户密码重置
export const updateUserPwd = (oldPassword: string, newPassword: string) => {
  return axios.put(`${prefix}/profile/updatePwd`, null, {
    params: { oldPassword, newPassword },
  });
};

// 修改用户个人信息
export const updateUserProfile = (data: UserProfile) => {
  return axios.put(`${prefix}/profile`, data);
};
export const resetPwd = (password: string, userId: number | string) => {
  return axios.put<IResponse<string>>(`system/user/resetPwd`, { password, userId });
};
export const updateLang = (lang: string) => axios.get(`v2/sys/api/lang/${lang}`);
